package com.hq.fxh.CustomerView;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SystemUtils;
import com.hq.fxh.Utils.Tool;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/14
 * @Email xlwenhao1858@sina.com
 */


public class PopupWindow_PingBi {
    PopupWindow pop;

    public void showPopupWindow(final Activity context, View popupView, View atView) {
        int[] location = new int[2];
        atView.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        int windowHeight = SystemUtils.getWindowHeight(context);
        int windowWidth = SystemUtils.getWindowWidth(context);

        LogUtils.e("x:" + x + "y:" + y + "   高度" + windowHeight);

        popupView.measure(0, 0);
        pop = new PopupWindow(popupView, windowWidth - DensityUtils.dip2px(context, 20), popupView.getMeasuredHeight(), true);
        pop.getHeight();
        pop.getWidth();

        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setAnimationStyle(R.style.dialog_anim_style);
        // 指定 PopupWindow 的背景
        pop.setFocusable(true);
        int height = pop.getHeight();
        LogUtils.e("pop高度" + height);

        //指定PopupWindow显示在你指定的view下

        if (windowHeight / 2 > (windowHeight - y)) {
            popupView.findViewById(R.id.popup_PingBi).setBackgroundResource(R.mipmap.tan_shou_to);

            pop.showAtLocation(atView, Gravity.TOP, 0, (y - height));

        } else {
          popupView.findViewById(R.id.popup_PingBi).setBackgroundResource(R.mipmap.tan_shou);
            //          popupView.findViewById(R.id.popup_PingBi).setBackgroundResource(R.drawable.btn_dropdown_normal);

            pop.showAtLocation(atView, Gravity.TOP, 0, (y + 40));
        }
        Tool.setAlpha((Activity) context, 0.6f);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);
            }
        });
    }

    public void dismissPopup() {
        pop.dismiss();

    }


}
